﻿using AppMachine.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppMachine.EntityFrameworkCore.EntityTypeConfiguration; 
public static class EntityTypeBuilderExtensions {

    public static void ConfigureAutoID<T>(this EntityTypeBuilder<T> b)
        where T : class, IHasAutoIncreasingID {
        b.As<EntityTypeBuilder>().TryConfigureConfigureAutoID();
    }
    public static void TryConfigureConfigureAutoID(this EntityTypeBuilder b) {
        if (b.Metadata.ClrType.IsAssignableTo<IHasAutoIncreasingID>()) {
            b.HasKey(nameof(IHasAutoIncreasingID.AutoID));
            b.HasIndex(nameof(IHasAutoIncreasingID.AutoID));
            b.Property(nameof(IHasAutoIncreasingID.AutoID));
        }
    }

    public static void ConfigureGlobalID<T>(this EntityTypeBuilder<T> b)
        where T : class, IHasGlobalID {
        b.As<EntityTypeBuilder>().TryConfigureGlobalID();
    }
    public static void TryConfigureGlobalID(this EntityTypeBuilder b) {
        if (b.Metadata.ClrType.IsAssignableTo<IHasGlobalID>()) {
            b.HasKey(nameof(IHasGlobalID.GlobalID));
            b.HasIndex(nameof(IHasGlobalID.GlobalID));
            b.Property(nameof(IHasGlobalID.GlobalID));
        }
    }

}
